﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;

namespace ATA.Winforms
{
    public partial class frmLogin : Form
    {
        ATA.Data.MisaDAL.DAL dal;

        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (dal.ValidateUser(txtUsername.Text, Utilities.EncodePassword(txtPassword.Text), cboChinhanh.Value.ToString()))
            {
                Globals.gv_strUsername = txtUsername.Text;
                Globals.gv_strBranchID = cboChinhanh.Value.ToString();
                Globals.gv_strBranchName = cboChinhanh.Text;
                Globals.gv_strBranchCode = cboChinhanh.SelectedRow.Cells["BranchCode"].Value.ToString();
                Globals.gv_bIsGlobalBranch = (bool)cboChinhanh.SelectedRow.Cells["IsSystem"].Value;
                this.Close();
            }
            else
            {
                MessageBox.Show("Tên người dùng hoặc mật khẩu không hợp lệ !!!");
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            #region Load chi nhanh
            dal = new ATA.Data.MisaDAL.DAL(ConfigurationManager.ConnectionStrings["misaConnectionString"].ConnectionString, ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
            DataSet v_dsBranches = dal.GetAllActiveBranchs();
            cboChinhanh.DataSource = v_dsBranches;
            cboChinhanh.DataMember = v_dsBranches.Tables[0].TableName;
            cboChinhanh.DisplayMember = "BranchName";
            cboChinhanh.ValueMember = "BranchID";
            cboChinhanh.SelectedRow = cboChinhanh.Rows[0];
            cboChinhanh.DisplayLayout.Bands[0].Columns["BranchID"].Hidden = true;
            cboChinhanh.DisplayLayout.Bands[0].Columns["IsSystem"].Hidden = true;
            cboChinhanh.DisplayLayout.Bands[0].Columns["BranchCode"].Header.Caption = "Mã";
            cboChinhanh.DisplayLayout.Bands[0].Columns["BranchCode"].Width = 60;
            cboChinhanh.DisplayLayout.Bands[0].Columns["BranchName"].Header.Caption = "Tên chi nhánh";
            cboChinhanh.DisplayLayout.Bands[0].Columns["BranchName"].Width = 192;
            cboChinhanh.DataBind();
            #endregion
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
